import visitor.StudentCountVisitor;

import java.util.HashMap;
import java.util.Map;

// Mutable class 维护已经被学生选择过的论文；若某论文未被选，则不包含在其中
public class PaperSelection {
    //该 Map 中，key 为论文，value 为学生，表明某论文被某学生选择了
    private final Map<Paper, Student> selections;

    public PaperSelection(PaperSelection selections) {
        this.selections = new HashMap<>();
    }

    //学生 student 选择了论文 paper，将其加入到 selections 中
    public synchronized boolean addSelection(Student student, Paper paper) {
        selections.put(paper, student);
        return true;
    }

    //检查论文 paper 是否已被学生选择过，若是，返回 true，否则 false
    public boolean isSelected(Paper paper) {
        return selections.containsKey(paper);
    }

    public Map<Paper, Student> getSelections() {
        return new HashMap<>(selections); // 返回防御性拷贝
    }

    public String toString() {
        StringBuilder str = new StringBuilder();
        for (Paper p : selections.keySet()) {
            str.append(p.toString()).append("\t selected by ").append(selections.get(p).getName()).append("\n");
        }
        return str.toString();
    }

    // 结合 Visitor 模式，添加 accept 方法
    public void accept(StudentCountVisitor visitor) {
        visitor.visit(this);
    }
}